﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace WeFrame.AutoCode
{
    /// <summary>
    /// 工具帮助类
    /// </summary>
    class UtilsHelper
    {

        /// <summary>
        /// 将字符传的第一个字母转换为小写
        /// </summary>
        /// <param Name="changeString">需要转换的字符串</param>
        /// <returns>转化后的字符串</returns>
        public static string LowerFirstChar(string changeString) 
        {
            return changeString.Substring(0, 1).ToLower() + changeString.Substring(1);
        }

        /// <summary>
        /// 将字符传的第一个字母转换为大写
        /// </summary>
        /// <param Name="changeString">需要转换的字符串</param>
        /// <returns>转化后的字符串</returns>
        public static string UpperFirstChar(string changeString)
        {
            return changeString.Substring(0, 1).ToUpper() + changeString.Substring(1);
        }

        /// <summary>
        /// 转化为驼峰命名
        /// </summary>
        /// <param name="change">原字符串</param>
        /// <returns>新字符串</returns>
        public static string ToCamelCase(string change)
        {
            return LowerFirstChar(ToPascalCase(change));
        }
            
        

        /// <summary>
        /// 转化为pascal命名
        /// </summary>
        /// <param name="change">原字符串</param>
        /// <returns>新字符串</returns>
        public static string ToPascalCase(string change)
        {
            if (change==null) return "";
            string[] arr = change.Split('_');
            string returnString="";
            for (int i = 0; i < arr.Count();i++ )
            {
                returnString += UpperFirstChar(arr[i]);
            }
            return returnString;
        }

        /// <summary>
        /// 创建制定长度空字符串
        /// </summary>
        /// <param Name="number">长度</param>
        /// <returns>符合长度的空字符</returns>
        public static string CreateBlankSpace(int number)
        {
            return new string(' ', number);
        }

        /// <summary>
        /// 
        /// 将数据库类型转化为代码数据类型（针对MySql和C#）
        /// </summary>
        /// <param Name="Name">数据类型名称</param>
        /// <returns>语言数据类型名称</returns>
        public static string DbTypeToCodeType(string name)
        {
            if (name == "int" || name == "smallint" || name == "bigint")
            {
                return "int";
            }
            else if (name == "bit")
            {
                return "byte";
            }
            else if (name == "numeric" || name == "real" || name == "float")
            {
                return "Single";
            }
            else if (name == "float")
            {
                return "float";
            }
            else if (name == "decimal")
            {
                return "decimal";
            }
            else if (name == "char" || name == "varchar" || name == "text" || name == "nchar" || name == "nvarchar" || name == "ntext")
            {
                return "string";
            }
            else if (name == "datetime" || name == "smalldatetime")
            {
                return "DateTime";
            }
            else if (name == "money" || name == "smallmoney")
            {
                return "double";
            }
            else
            {
                return "string";
            }
        }

        /// <summary>
        /// 获得产生信息
        /// </summary>
        /// <returns></returns>
        public static string GetAutoCodeInformation()
        {
            return "time:"+DateTime.Now + " Author:neo";
        }


        /// <summary>
        /// 构造数据库查询参数
        /// </summary>
        /// <param name="queryParamList">查询参数集合</param>
        /// <returns>数据库查询参数数组</returns>
        public static SqlParameter[] BuildSqlParameters(List<QueryParam> queryParamList) 
        {
            SqlParameter[] parameters = new SqlParameter[queryParamList.Count];
            for (int i = 0; i < queryParamList.Count; i++)
            {
                QueryParam queryParam = queryParamList[i];
                parameters[i] = new SqlParameter("@" + queryParam.ParamName, queryParam.ParamValue);
            }
            return parameters;
        }


        /// <summary>
        /// 构造查询条件
        /// </summary>
        /// <param name="queryParamList">查询参数集合</param>
        /// <returns>查询条件字符串</returns>
        public static string BuildWhere(List<QueryParam> queryParamList) 
        {
            string where = "";
            SqlParameter[] parameters = new SqlParameter[queryParamList.Count];
            for (int i = 0; i < queryParamList.Count; i++)
            {
                QueryParam queryParam = queryParamList[i];
                where = where + " AND " + queryParam.ParamName + " " + queryParam.Operation + " " + "@" + queryParam.ParamName;
            }
            return where;
        }

        /// <summary>
        /// 构造查询数据sql
        /// </summary>
        /// <param name="TableName">表名</param>
        /// <param name="queryParamList">查询参数集合</param>
        /// <param name="queryOrderList">查询排序集合</param>
        /// <param name="queryPage">分页</param>
        /// <returns>sql</returns>
        public static string BuildQueryPageSql(string tableName,List<QueryParam> queryParamList, List<QueryOrder> queryOrderList, QueryPage queryPage)
        {

            string where = "";
            string order = "";
            string sql = "";
            where = BuildWhere(queryParamList);
            for (int j = 0; j < queryOrderList.Count; j++)
            {
                QueryOrder queryOrder = queryOrderList[j];
                order = order + "ORDER BY " + queryOrder.OrderName + " " + queryOrder.OrderWay;
                if ((j + 1) != queryOrderList.Count) where = where + ",";
            }
            sql = "SELECT ROW_NUMBER() OVER(" + order + ") AS RowNumber,* FROM "+tableName+" " + where;
            sql = "SELECT * FROM (" + sql + ") AS T WHERE RowNumber BETWEEN " + ((queryPage.PageIndex - 1) * queryPage.PageSize + 1) + " AND " + (queryPage.PageIndex) * queryPage.PageSize;
            
            return sql;
        }

        /// <summary>
        /// 去掉最后一个字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RemoveTheLastCharacte(string str)
        {
            if (string.IsNullOrEmpty(str)) return "";
            return str.Substring(0, str.Length - 1);
        }

    }
}
